package org.dromara.plotInfo.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.plotInfo.domain.bo.LandPlotInfoBo;
import org.dromara.plotInfo.domain.vo.LandPlotInfoVo;
import org.dromara.plotInfo.domain.LandPlotInfo;
import org.dromara.plotInfo.mapper.LandPlotInfoMapper;
import org.dromara.plotInfo.service.ILandPlotInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 地块查询Service业务层处理
 *
 * @author Lion Li
 * @date 2024-12-12
 */
@RequiredArgsConstructor
@Service
public class LandPlotInfoServiceImpl implements ILandPlotInfoService {

    private final LandPlotInfoMapper baseMapper;

    /**
     * 查询地块查询
     *
     * @param plotId 主键
     * @return 地块查询
     */
    @Override
    public LandPlotInfoVo queryById(Long plotId){
        return baseMapper.selectVoById(plotId);
    }

    /**
     * 分页查询地块查询列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 地块查询分页列表
     */
    @Override
    public TableDataInfo<LandPlotInfoVo> queryPageList(LandPlotInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LandPlotInfo> lqw = buildQueryWrapper(bo);
        Page<LandPlotInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的地块查询列表
     *
     * @param bo 查询条件
     * @return 地块查询列表
     */
    @Override
    public List<LandPlotInfoVo> queryList(LandPlotInfoBo bo) {
        LambdaQueryWrapper<LandPlotInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<LandPlotInfo> buildQueryWrapper(LandPlotInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LandPlotInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBoundaryCoordinates()), LandPlotInfo::getBoundaryCoordinates, bo.getBoundaryCoordinates());
        lqw.eq(StringUtils.isNotBlank(bo.getCenterCoordinates()), LandPlotInfo::getCenterCoordinates, bo.getCenterCoordinates());
        lqw.eq(bo.getAreaSize() != null, LandPlotInfo::getAreaSize, bo.getAreaSize());
        lqw.eq(StringUtils.isNotBlank(bo.getLandUse()), LandPlotInfo::getLandUse, bo.getLandUse());
        lqw.like(StringUtils.isNotBlank(bo.getOwnerName()), LandPlotInfo::getOwnerName, bo.getOwnerName());
        lqw.eq(StringUtils.isNotBlank(bo.getOwnerType()), LandPlotInfo::getOwnerType, bo.getOwnerType());
        lqw.eq(StringUtils.isNotBlank(bo.getLoginIp()), LandPlotInfo::getLoginIp, bo.getLoginIp());
        lqw.eq(bo.getLoginDate() != null, LandPlotInfo::getLoginDate, bo.getLoginDate());
        return lqw;
    }

    /**
     * 新增地块查询
     *
     * @param bo 地块查询
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(LandPlotInfoBo bo) {
        LandPlotInfo add = MapstructUtils.convert(bo, LandPlotInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPlotId(add.getPlotId());
        }
        return flag;
    }

    /**
     * 修改地块查询
     *
     * @param bo 地块查询
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(LandPlotInfoBo bo) {
        LandPlotInfo update = MapstructUtils.convert(bo, LandPlotInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LandPlotInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除地块查询信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
